
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>JavaScript Promise </h1>
<p>在学习本章节内容前，你需要先了解什么是异步编程，可以参考：<a  rel="noopener noreferrer" target="_blank">JavaScript 异步编程</a></p>
<p>
Promise 是一个 ECMAScript 6 提供的类，目的是更加优雅地书写复杂的异步任务。</p>
<p>由于 Promise 是 ES6 新增加的，所以一些旧的浏览器并不支持，苹果的 Safari 10 和 Windows 的 Edge 14 版本以上浏览器才开始支持 ES6 特性。</p><p>
以下是 Promise 浏览器支持的情况：</p>
<table class="browserref">
<tbody><tr style="height:50px">
<th class="bsChrome" style="width:16%;" title="Chrome"></th>
<th class="bsEdge" style="width:16%;" title="Edge"></th>
<th class="bsFirefox" style="width:16%;" title="Firefox"></th>
<th class="bsSafari" style="width:16%;" title="Safari"></th>
<th class="bsOpera" style="width:16%;" title="Opera"></th>
</tr>
<tr>
<td>Chrome 58</td>
<td>Edge 14</td>
<td>Firefox 54</td>
<td>Safari 10</td>
<td>Opera 55</td>
</tr>
</tbody></table>
<h3>
构造 Promise</h3>
<p>现在我们新建一个 Promise 对象：</p>
<pre>new Promise(function (resolve, reject) {
    // 要做的事情...
});</pre>
<p>通过新建一个 Promise 对象好像并没有看出它怎样 "更加优雅地书写复杂的异步任务"。我们之前遇到的异步任务都是一次异步，如果需要多次调用异步函数呢？例如，如果我想分三次输出字符串，第一次间隔 1 秒，第二次间隔 4 秒，第三次间隔 3 秒：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">First</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Second</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Third</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">3000</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">4000</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">1000</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>这段程序实现了这个功能，但是它是用 "函数瀑布" 来实现的。可想而知，在一个复杂的程序当中，用 "函数瀑布" 实现的程序无论是维护还是异常处理都是一件特别繁琐的事情，而且会让缩进格式变得非常冗赘。</p>
<p>现在我们用 Promise 来实现同样的功能：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">First</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">1000</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Second</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">4000</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Third</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">3000</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>这段代码较长，所以还不需要完全理解它，我想引起注意的是 Promise 将嵌套格式的代码变成了顺序格式的代码。</p>
<h3>Promise 的构造函数</h3> <p>Promise 构造函数是 JavaScript 中用于创建 Promise 对象的内置构造函数。 </p><p>
Promise 构造函数接受一个函数作为参数，该函数是同步的并且会被立即执行，所以我们称之为起始函数。起始函数包含两个参数 resolve 和 reject，分别表示 Promise 成功和失败的状态。</p>
<p>起始函数执行成功时，它应该调用 resolve 函数并传递成功的结果。当起始函数执行失败时，它应该调用 reject 函数并传递失败的原因。</p>
<p>Promise 构造函数返回一个 Promise 对象，该对象具有以下几个方法：</p>
<ul><li>
then：用于处理 Promise 成功状态的回调函数。</li><li>
catch：用于处理 Promise 失败状态的回调函数。</li><li>
finally：无论 Promise 是成功还是失败，都会执行的回调函数。</li></ul>
<p>下面是一个使用 Promise 构造函数创建 Promise 对象的例子：

<p>
当 Promise 被构造时，起始函数会被同步执行：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">promise</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> =&gt; </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 异步操作</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> =&gt; </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> &lt; </span><span class="hl-number">0.5</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">success</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">reject</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">error</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-number">1000</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-identifier">promise</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-identifier">result</span><span class="hl-code"> =&gt; </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">result</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">error</span><span class="hl-code"> =&gt; </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">error</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>在上面的例子中，我们使用 Promise 构造函数创建了一个 Promise 对象，并使用 setTimeout 模拟了一个异步操作。如果异步操作成功，则调用 resolve 函数并传递成功的结果；如果异步操作失败，则调用 reject 函数并传递失败的原因。然后我们使用 then 方法处理 Promise 成功状态的回调函数，使用 catch 方法处理 Promise 失败状态的回调函数。</p>
<p>这段程序会直接输出 <strong>error</strong> 或 <strong>success</strong>。</p>
<p>resolve 和 reject 都是函数，其中调用 resolve 代表一切正常，reject 是出现异常时所调用的：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">reject</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Divide zero</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> / </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">a / b = </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-reserved">finally</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">End</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>这段程序执行结果是:</p>
<pre>a / b = 0
End</pre>
<p>Promise 类有 .then() .catch() 和 .finally() 三个方法，这三个方法的参数都是一个函数，.then() 可以将参数中的函数添加到当前 Promise 的正常执行序列，.catch() 则是设定 Promise 的异常处理序列，.finally() 是在 Promise 执行的最后一定会执行的序列。
.then() 传入的函数会按顺序依次执行，有任何异常都会直接跳到 catch 序列：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-number">1111</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-number">2222</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">3333</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">An error</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>执行结果：</p>
<pre>1111
2222
3333
An error</pre>
<p>resolve() 中可以放置一个参数用于向下一个 then 传递一个值，then 中的函数也可以返回一个值传递给 then。但是，如果 then 中返回的是一个 Promise 对象，那么下一个 then 将相当于对这个返回的 Promise 进行操作，这一点从刚才的计时器的例子中可以看出来。</p>
<p>reject() 参数中一般会传递一个异常给之后的 catch 函数用于处理异常。</p>
<p>但是请注意以下两点：</p><ul><li>
resolve 和 reject 的作用域只有起始函数，不包括 then 以及其他序列；</li><li>
resolve 和 reject 并不能够使起始函数停止运行，别忘了 return。</li></ul>
<h3>Promise 函数</h3><p>
上述的 "计时器" 程序看上去比函数瀑布还要长，所以我们可以将它的核心部分写成一个 Promise 函数：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">delay</span><span class="hl-code">, </span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div><p>
然后我们就可以放心大胆的实现程序功能了：</p><div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">1000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">First</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">4000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Second</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">3000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Third</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>这种返回值为一个 Promise 对象的函数称作 Promise 函数，它常常用于开发基于异步操作的库。</p>
<h3>回答常见的问题（FAQ）</h3>
<p><strong>Q: then、catch 和 finally 序列能否顺序颠倒？</strong></p><p>
A: 可以，效果完全一样。但不建议这样做，最好按 then-catch-finally 的顺序编写程序。</p>
<p><strong>Q: 除了 then 块以外，其它两种块能否多次使用？</strong></p><p>
A: 可以，finally 与 then 一样会按顺序执行，但是 catch 块只会执行第一个，除非 catch 块里有异常。所以最好只安排一个 catch 和 finally 块。</p>
<p><strong>Q: then 块如何中断？</strong></p><p>
A: then 块默认会向下顺序执行，return 是不能中断的，可以通过 throw 来跳转至 catch 实现中断。</p>
<p><strong>Q: 什么时候适合用 Promise 而不是传统回调函数？</strong></p><p>
A: 当需要多次顺序执行异步操作的时候，例如，如果想通过异步方法先后检测用户名和密码，需要先异步检测用户名，然后再异步检测密码的情况下就很适合 Promise。</p>
<p><strong>Q: Promise 是一种将异步转换为同步的方法吗？</strong></p><p>
A: 完全不是。Promise 只不过是一种更良好的编程风格。</p>
<p><strong>Q: 什么时候我们需要再写一个 then 而不是在当前的 then 接着编程？</strong></p><p>
A: 当你又需要调用一个异步任务的时候。</p>
<h3>异步函数</h3><p>
异步函数（async function）是 ECMAScript 2017 (ECMA-262) 标准的规范，几乎被所有浏览器所支持，除了 Internet Explorer。</p><p>
在 Promise 中我们编写过一个 Promise 函数：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">delay</span><span class="hl-code">, </span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">setTimeout</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>
<p>然后用不同的时间间隔输出了三行文本：</p><div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">1000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">First</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">4000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Second</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">3000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Third</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>我们可以将这段代码变得更好看：</p><div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">async</span><span class="hl-code"> </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">await</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">1000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">First</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">await</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">4000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Second</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">await</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">3000</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Third</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>哈！这岂不是将异步操作变得像同步操作一样容易了吗！</p><p>
这次的回答是肯定的，异步函数 async function 中可以使用 await 指令，await 指令后必须跟着一个 Promise，异步函数会在这个 Promise 运行中暂停，直到其运行结束再继续运行。</p><p>
异步函数实际上原理与 Promise 原生 API 的机制是一模一样的，只不过更便于程序员阅读。</p><p>
处理异常的机制将用 try-catch 块实现：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">async</span><span class="hl-code"> </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">await</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Some error</span><span class="hl-quotes">"</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 或者 reject("Some error")</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">err</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 会输出 Some error</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div><p>
如果 Promise 有一个正常的返回值，await 语句也会返回它：</p><div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">async</span><span class="hl-code"> </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">value</span><span class="hl-code"> = </span><span class="hl-identifier">await</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Promise</span><span class="hl-brackets">(</span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">resolve</span><span class="hl-code">, </span><span class="hl-identifier">reject</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Return value</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">value</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">asyncFunc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p>程序会输出:</p>
<pre>Return value</pre>
<hr/>
<h2>更多内容</h2>
<a  rel="noopener noreferrer" target="_blank">JavaScript Promise 对象</a> <div class="scroll-top"><a >返回顶部</a></div>
</p></div>

    </body>
    </html>
    